<?php

/* Class name:  SQLTime
 * Description: formates time between SQL time data-types (DATETIME, DATE, TIME)
 *              and UNIX time stamp
 * Author:      Michal "Techi" Vrchota 2004
 *              michal.vrchota@seznam.cz
 *              http://techi.iglu.cz/
 *
 * Patched by Svilen Spasov
 *								<svilen@svilen.com>
 *		added PostGreSQL Time support
 
 	example:
  	Convert SQL DATETIME to UNIX time stamp or formatted time by date();
    --------------------------------------------------------------------
 		SQLTime::Time2Stamp($date, 'l, F j S, Y; g:i A');  
    returns:
    	 	Friday, June 10th, 2005; 2:27 PM
        
    SQLTime::Time2Stamp($date);
    returns:
    		1932136786;
        
    Converts UNIX time stamp to SQL-DATETIME
    ----------------------------------------
 		SQLTime::Stamp2DATETIME($stamp);
    
    // converts UNIX time stamp to SQL-DATE
		SQLTime::Stamp2DATE($unix_stamp);
    
    // converts UNIX time stamp to SQL-TIME
    SQLTime::Stamp2TIME($unix_stamp);
    
    // returns Day difference in days
    SQLTime::DiffDays($day1, $day2);
    
    // returns GMT time
    SQLTime::GMT_Time($Timestamp, 1);
    
    example time:
    $TimeZoneOffset = -2;
    date("F d, Y; H:i:s", SQLTime::GMT_Time(time(), $TimeZoneOffset)).' GMT';
  
    example timestamp:
    SQLTime::GMT_Time($Timestamp, $TimeZoneOffset);
 */

define("sec_per_day", 86400);

class SQLTime
{
	// converts SQL datetime format to UNIX time stamp
	// or format by Date() if the second parameter is specified
	function Time2Stamp($datetime, $format = "")
	{
		switch(strlen($datetime))
		{
			// DATETIME
			case 26:
        $year = IntVal(SubStr($datetime, 0, 4));
		 		$month = IntVal(SubStr($datetime, 5, 2));
		 		$day = IntVal(SubStr($datetime, 8, 2));
				$hour = IntVal(SubStr($datetime, 11, 2));
				$minute = IntVal(SubStr($datetime, 14, 2));
				$second = IntVal(SubStr($datetime, 17, 2));

				$stamp = MkTime($hour, $minute, $second, $month, $day, $year);
				if($stamp == -1)
					return FALSE;

				break;
        
      // DATETIME
			case 19:
        $year = IntVal(SubStr($datetime, 0, 4));
		 		$month = IntVal(SubStr($datetime, 5, 2));
		 		$day = IntVal(SubStr($datetime, 8, 2));
				$hour = IntVal(SubStr($datetime, 11, 2));
				$minute = IntVal(SubStr($datetime, 14, 2));
				$second = IntVal(SubStr($datetime, 17, 2));

				$stamp = MkTime($hour, $minute, $second, $month, $day, $year);
				if($stamp == -1)
					return FALSE;

				break;

			// DATE
			case 10:

			 	$year = IntVal(SubStr($datetime, 0, 4));
 				$month = IntVal(SubStr($datetime, 5, 2));
 				$day = IntVal(SubStr($datetime, 8, 2));

				$stamp = MkTime(0, 0, 0, $month, $day, $year);
				if($stamp == -1)
					return FALSE;

				break;

			// TIME
			case 8:

				$hour = IntVal(SubStr($datetime, 0, 2));
				$minute = IntVal(SubStr($datetime, 3, 2));
				$second = IntVal(SubStr($datetime, 6, 2));

				$stamp =  MkTime($hour, $minute, $second, 1, 1, 1970);
				if($stamp == -1)
					return FALSE;
					
				break;

			//default:
				//return FALSE;
		}

		if($format == "")
			return $stamp;
		else
			return Date($format, $stamp);
	}

	// converts UNIX time stamp to SQL-DATETIME
	function Stamp2DATETIME($stamp)
	{
		return Date("Y-m-d H:i:s", $stamp);
	}

	// converts UNIX time stamp to SQL-DATE
	function Stamp2DATE($stamp)
	{
		return Date("Y-m-d", $stamp);
	}

	// converts UNIX time stamp to SQL-TIME
	function Stamp2TIME($stamp)
	{
		return Date("H:i:s", $stamp);
	}

	// returns Day difference
	function DiffDays($day1, $day2)
	{
		return IntVal(abs($day2 - $day1) / sec_per_day);
	}
  
  function GMT_Time($Stamp,	$Change)
  {
		return (($Stamp == 0) ? time() : SQLTime::Time2Stamp($Stamp)) + $Change * 3600; 
	}
}

?>